package com.yjy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.system.comm.util.JsonUtils;
import com.system.common.pojo.SysDept;
import com.system.common.pojo.SysMenu;
import com.system.common.pojo.SysUser;
import com.yjy.service.SysDeptService;
import com.yjy.service.SysRoleService;

@Controller
@RequestMapping("dept")
public class SysDeptController {
	
	@Autowired
	private SysDeptService deptService;
	
	@Autowired
	private SysRoleService roleService;
	
	@RequestMapping("list")
	public String list(Model model,HttpServletRequest request,HttpSession session){
		Map<String, Object> map=new HashMap<>();
		String dname=request.getParameter("dname");
		if (dname!=null) {
			map.put("dname", dname);
		}
		SysUser user=(SysUser) session.getAttribute("user");		
		List<SysMenu> power=roleService.selectPower(user.getUserId());
		String menus="";
		for (SysMenu menu : power) {
			menus+=menu.getUrl()+",";
		}
		model.addAttribute("power", menus);
		model.addAttribute("deptList", deptService.listAll(map));
		return "sys/sysdept";
	}
	
	@RequestMapping(value="tooDept",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String tooDept(HttpServletResponse response){
		 Map<String, Object>  map=new HashMap<>();
		 List<SysDept>  depts=deptService.listAll(map);
		 String dept= JsonUtils.toJson(depts);
		 return dept;
	}
	
	@RequestMapping("delete")
	public String delete(String deptId){
		deptService.del(deptId);
		return "redirect:/dept/list";
		
	}
	
	@RequestMapping("save")
	public String save(SysDept dept,HttpSession session){
		SysUser user=(SysUser) session.getAttribute("user");
		dept.setCreateUserId(user.getUserId());
	    deptService.save(dept);
		return "redirect:/dept/list";
		
	}
	@RequestMapping("update")
	public String update(SysDept dept,HttpSession session){
		SysUser user=(SysUser) session.getAttribute("user");
		dept.setModifyUserId(user.getUserId());
		deptService.update(dept);
		return "redirect:/dept/list";
		
	}
	@RequestMapping(value="toselectParentname",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String toselectParentname(String parentId){
		String parentName=deptService.selectParentname(parentId);
		return parentName;
		
	}
	@RequestMapping(value="toupdate",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String toupdate(String deptId){
		SysDept sysDept=deptService.getById(deptId);
		String depts=JsonUtils.toJson(sysDept);
		return depts;
	}
	/**
	 * 移动机构
	 * @param deptId
	 * @return
	 */
	@RequestMapping("travelMechanism")
	public String travelMechanism(String  deptIds,String parentIds){//获取的第一个参数是要切换父节点的id，第二个参数要切换到哪个父节点下面
	     deptService.move(deptIds, parentIds);
		return "redirect:/dept/list";
		
	}
	/**
	 * 根据菜单树的id进行查询
	 * @param deptIds
	 * @param parentIds
	 * @return
	 */
	@RequestMapping("queryDeptIds")
	public String queryDeptIds(String ids,Model model){
	    List<SysDept>  depts= deptService.queryDeptIds(ids);
	    model.addAttribute("deptList", depts);
		return "sys/sysdept";
		
	}
}
